﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DSheet.Core.CommonData;
using DSheet.Core;

namespace DExcelServiceApp
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (DBContext context = new DBContext())
                {
                    List<BDomain> domains = new CommonDataService().GetDomains(context);
                    _ctlListDomain.DataSource = domains;
                    _ctlListDomain.DataBind();
                    if (domains.Count > 0)
                    {
                        this._ctlHiddenDomain.Value = domains.First().Id.ToString();
                    }

                    List<BOrg> orgs = new CommonDataService().GetOrgs(context);
                    _ctlListOrg.DataSource = orgs;
                    _ctlListOrg.DataBind();
                    if (orgs.Count > 0)
                    {
                        this._ctlHiddenOrg.Value = orgs.First().Id.ToString();
                    }

                    List<BYear> years = new CommonDataService().GetYears(context);
                    _ctlListYear.DataSource = years;
                    _ctlListYear.DataBind();
                    if (years.Count > 0)
                    {
                        this._ctlHiddenYear.Value = years.First().Id.ToString();
                    }
                }
            }
        }

        protected void _ctlListYear_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid yearId = new Guid(this._ctlListYear.SelectedValue);
            this._ctlHiddenYear.Value = yearId.ToString();
        }

        protected void _ctlListDomain_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid domainId = new Guid(this._ctlListDomain.SelectedValue);
            this._ctlHiddenDomain.Value = domainId.ToString();
        }

        protected void _ctlListOrg_SelectedIndexChanged(object sender, EventArgs e)
        {
            Guid orgId = new Guid(this._ctlListOrg.SelectedValue);
            this._ctlHiddenOrg.Value = orgId.ToString();
        }

        protected void _ctlBtnEnter_Click(object sender, EventArgs e)
        {
            Guid yearId = new Guid(this._ctlListYear.SelectedValue);
            this._ctlHiddenYear.Value = yearId.ToString();
            Guid domainId = new Guid(this._ctlListDomain.SelectedValue);
            this._ctlHiddenDomain.Value = domainId.ToString();
            Guid orgId = new Guid(this._ctlListOrg.SelectedValue);
            this._ctlHiddenOrg.Value = orgId.ToString();

            SessionData sd = Session["sd"] as SessionData;
            sd.ORGID = orgId;
            sd.BDID = domainId;
            sd.YearID = yearId;
            Response.Redirect("./DimDocTemplateList.aspx");
        }
    }
}